#include "PhoneGapForm.h"

#include <FApp.h>

using namespace Osp::Base;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;
using namespace Osp::System;
using namespace Osp::Graphics;
using namespace Osp::Web::Controls;

PhoneGapForm::PhoneGapForm(void)
{
	__requests.Construct();
	__isNativeReady = false;

	__pPluginManager = NULL;
	__pWeb = NULL;
}

PhoneGapForm::~PhoneGapForm(void)
{
}

bool
PhoneGapForm::Initialize()
{
	return true;
}

result
PhoneGapForm::OnInitializing(void)
{
	result r = E_SUCCESS;
	int screen_width = 0;
	int screen_height = 0;

	/*screen*/
    if(E_SUCCESS == r) r = SystemInfo::GetValue("ScreenWidth", screen_width);
    if(E_SUCCESS == r) r = SystemInfo::GetValue("ScreenHeight", screen_height);

    /*Web*/
    if(NULL == __pWeb) __pWeb = new Osp::Web::Controls::Web();
    if(NULL == __pWeb) r = E_OUT_OF_MEMORY;
	if(E_SUCCESS == r) r = __pWeb->Construct(Rectangle(0, 0, screen_width, screen_height));
	if(E_SUCCESS == r) r = AddControl(*__pWeb);
	if(E_SUCCESS == r) r = __pWeb->SetFocus();
	if(E_SUCCESS == r) __pWeb->SetLoadingListener(this);

	if(NULL == __pPluginManager) __pPluginManager = new PluginManager();
	if(NULL == __pPluginManager) r = E_OUT_OF_MEMORY;
	if(E_SUCCESS == r) __pPluginManager->Construct(this);
	return r;
}

result
PhoneGapForm::OnTerminating(void)
{
	result r = E_SUCCESS;

	__pWeb->StopLoading();
	RemoveAllControls();
	__pWeb = NULL;

	if(0 < __requests.GetCount())
			__requests.RemoveAll();

	if(NULL != __pPluginManager)
	{
		delete __pPluginManager;
		__pPluginManager = NULL;
	}

	return r;
}

bool
PhoneGapForm::OnLoadingRequested (const Osp::Base::String &url, Osp::Web::Controls::WebNavigationType type)
{
	//////////////printLog(L"Request URL: " + url);

	if(true == url.StartsWith(L"gap://",0))
	{
		__requests.Enqueue(url);
	}
	else
	{
		__isNativeReady = false;
	}
	return false;
}

void
PhoneGapForm::OnLoadingCompleted(void)
{
	/* native ready */
	if(__isNativeReady == false)
	{
		nativeReady();
		__isNativeReady = true;
	}

	while(0 < __requests.GetCount())
	{
		String request;
		__requests.Dequeue(request);
		__pPluginManager->processRequest(request);
	}	// while
}

Osp::Base::String
PhoneGapForm::evaluateJavascript(Osp::Base::String javascript)
{
	Osp::Base::String * pValue = NULL;
	Osp::Base::String value;

	//////////////////printLog(L"Evaluate Javascript : " + javascript);

	if(NULL != __pWeb)
	{
		pValue = __pWeb->EvaluateJavascriptN(javascript);

		if(NULL != pValue)
		{
			value = *pValue;
			delete pValue;
		}
	}

	return value;
}

void
PhoneGapForm::LoadUrl(Osp::Base::String url)
{
	__pWeb->LoadUrl(url);
}

result
PhoneGapForm::nativeReady()
{
	return __pPluginManager->nativeReady();
}

result
PhoneGapForm::exec(Osp::Base::String service,Osp::Base::String action,Osp::Base::String callbackId,Osp::Base::String jsonArgs,bool isAsync)
{
	return __pPluginManager->exec(service,action,callbackId,jsonArgs,isAsync);
}

